package com.qc31.comlib.net

import android.content.Intent
import com.qc31.baselib.net.AppContext
import com.qc31.baselib.net.NetWorkApi
import com.qc31.baselib.net.errorhandler.ExceptionHandle
import com.qc31.comlib.R
import com.qc31.comlib.UrlUtil
import com.qc31.comlib.UserConstant
import com.qc31.comlib.entity.HBaseBean
import io.reactivex.rxjava3.core.SingleTransformer
import io.reactivex.rxjava3.functions.Function
import okhttp3.FormBody
import okhttp3.Interceptor
import java.nio.charset.Charset


/** @Author pc_hfjs @Date 2022/1/18 12:02 @Desc */
class Net private constructor() : NetWorkApi() {
    override fun <T> getAppErrorHandler(): Function<T, T> {
        return Function(fun(it: T): T {

            if (it is HBaseBean) {
                val result = it as HBaseBean
                if (result.status != 1) {
                    if (result.status == -1 || result.status == -2) {

//                        AppContext.getAppContext().startActivity(
//                            Intent(AppContext.getAppContext(), LoginActivity::class.java)
//                                .putExtra("login", true)
//                                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                        )

                        throw ExceptionHandle.ServerException(
                            0,
                            if (result.result == null) {
                                AppContext.getDescById(R.string.toast_no_response)
                            } else {
                                AppContext.getDescById(R.string.toast_no_login_again)
                            }
                        )
                    } else {
                        throw ExceptionHandle.ServerException(
                            0,
                            if (result.result == null) {
                                AppContext.getDescById(R.string.toast_no_response)
                            } else {
                                result.result.toString()
                            }
                        )
                    }
                }
            }
            return it
        })
    }

    //添加公共参数
    override fun getInterceptor(): Interceptor {
        return Interceptor { chain ->
            var request = chain.request()
            if (request.body is FormBody) {
                val bodyBuilder: FormBody.Builder = FormBody.Builder(Charset.forName("UTF-8"))
                val body = request.body as? FormBody
                body?.let {
                    for (i in 0 until it.size) {
                        bodyBuilder.addEncoded(it.encodedName(i), it.encodedValue(i))
                    }
                }
                bodyBuilder.addEncoded("sessionId", UserConstant.sessionId)

                request = request.newBuilder()
//                    .addHeader("Authorization", UserConstant.sessionId)
                    .post(bodyBuilder.build()).build()
            } else {
                val bodyBuilder: FormBody.Builder = FormBody.Builder(Charset.forName("UTF-8"))
                bodyBuilder.addEncoded("sessionId", UserConstant.sessionId)
                request = request.newBuilder()
//                    .addHeader("Authorization", UserConstant.sessionId)
                    .post(bodyBuilder.build()).build()
            }
            chain.proceed(request)
        }
    }

    override fun testUrl(): String {
        return UrlUtil.newBase()/*"http://192.168.2.91:8080/gps-web/app/"*/
    }

    override fun formalUrl(): String {
        return UrlUtil.newBase()
    }

    companion object {
        private val instance by lazy { Net() }

        fun <T> getService(service: Class<T>): T {
            return instance.getRetrofit(service).create(service)
        }

        fun <T> getServiceNoParams(service: Class<T>, url: String): T {
            return instance.getRetrofitNoParams(service, url).create(service)
        }

        fun <T> getServiceParams(service: Class<T>, url: String): T {
            return instance.getRetrofitParams(service, url).create(service)
        }

        fun <T> getServiceNoParams(service: Class<T>): T {
            return instance.getRetrofitNoParams(service).create(service)
        }

        fun <T> applySingleSchedulers(): SingleTransformer<T, T> {
            return instance.applySingleSchedulers()
        }

        fun <T> applyNoMapSingleSchedulers(): SingleTransformer<T, T> {
            return instance.applyNoMapSingleSchedulers()
        }

        fun init(isDebug: Boolean) {
            instance.init(isDebug)
        }

        fun changeUrl() {
            instance.changeUrl()
        }
    }
}